import java.awt.Point;
import java.awt.event.KeyEvent;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.script.Calculations;
import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.script.wrappers.RSPlayer;

public class MysticAntiban extends Random {

	long lastTime = System.currentTimeMillis();

	public double getVersion() {
		return 1.0;
	}

	// Lets rest
	public void rest() {
		clickMouse(random(707, 762), random(90, 121), false);
		wait(random(80, 120));
		atMenu("rest");
		waitForAnim(1000);
	}

	// Check my skills
	public void checkMySkill() {
		if (getCurrentTab() != TAB_INVENTORY) {
			openTab(TAB_STATS);
			moveMouse(random(557, 598), random(229, 252));
		}
	}

	// Checks a Player
	public void checkPlayer() {
		RSPlayer observe = getNearestPlayerByLevel(3, 138);
		Point observePos = Calculations.tileToScreen(observe.getLocation());
		if (observe != null && tileOnScreen(observe.getLocation())) {
			long time = System.currentTimeMillis() + random(4000, 8000);
			int nmbr = random(0, 2);
			if (nmbr == 0) {
				while (System.currentTimeMillis() < time && !isInventoryFull()) {
					if (!pointOnScreen(observePos)) {
						break;
					}
					dragMouse(observePos);
				}
			} else {
				if (!pointOnScreen(observePos)) {
					return;
				}
				clickMouse(observePos, false);
				wait(random(2000, 5000));
				while (isMenuOpen())
					moveMouseRandomly(250);
			}
		}
	}

	// Turns the camera random
	public void doCamera() {
		char[] LR = new char[] { KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT };
		char[] UD = new char[] { KeyEvent.VK_DOWN, KeyEvent.VK_UP };
		char[] LRUD = new char[] { KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,
				KeyEvent.VK_UP, KeyEvent.VK_UP };
		int random2 = random(0, 2);
		int random1 = random(0, 2);
		int random4 = random(0, 4);

		if (random(0, 3) == 0) {
			Bot.getInputManager().pressKey(LR[random1]);
			try {
				Thread.sleep(random(100, 400));
			} catch (Exception e) {
			}
			Bot.getInputManager().pressKey(UD[random2]);
			try {
				Thread.sleep(random(300, 600));
			} catch (Exception e) {
			}
			Bot.getInputManager().releaseKey(UD[random2]);
			try {
				Thread.sleep(random(100, 400));
			} catch (Exception e) {
			}
			Bot.getInputManager().releaseKey(LR[random1]);
		} else {
			Bot.getInputManager().pressKey(LRUD[random4]);
			if (random4 > 1) {
				try {
					Thread.sleep(random(300, 600));
				} catch (Exception e) {
				}
			} else {
				try {
					Thread.sleep(random(500, 900));
				} catch (Exception e) {
				}
			}
			Bot.getInputManager().releaseKey(LRUD[random4]);
		}
	}

	// Opens random tabs
	public void openRandomTab() {
		openTab(random(0, 14));
	}

	// Checks my FriendsList
	public void checkMyFriends() {
		openTab(TAB_FRIENDS);
		moveMouse(random(554, 709), random(227, 444));
	}

	// myAntiban
	private int myAntiban() {
		int roll = (int) (Math.random() * 1000);
		if (roll > 995) {
			if (getEnergy() > 50 && !isRunning() && !getMyPlayer().isMoving()
					&& !(getMyPlayer().getAnimation() == 11786)
					|| !(getMyPlayer().getAnimation() == 11788)
					|| !(getMyPlayer().getAnimation() == 5713)
					|| !(getMyPlayer().getAnimation() == 5748)) {
				rest();
			} else {
				rest();
				while (getEnergy() < 100
						&& (getMyPlayer().getAnimation() == 11786)
						|| (getMyPlayer().getAnimation() == 11788)
						|| (getMyPlayer().getAnimation() == 5713)
						|| (getMyPlayer().getAnimation() == 5748)) {
					myAntiban();
					wait(15);
				}
				wait(random(300, 800));
			}

		} else if (roll > 990 && getInventoryCount() < 23) {
			checkMySkill();
		} else if (roll > 985 && getInventoryCount() < 23) {
			checkPlayer();
		} else if (roll > 960) {
			doCamera();
		} else if (roll > 940) {
			moveMouseSlightly();
		} else if (roll > 890) {
			openRandomTab();
		} else if (roll > 780) {
			checkMyFriends();
		}
		return 500;
	}

	public String getAuthor() {
		return "Mouchicc - Mystic Team";
	}

	@Override
	public boolean activateCondition() {
		int roll = (int) (Math.random() * 1000);
		if (roll > 995) {
			if (getEnergy() > 50 && !isRunning() && !getMyPlayer().isMoving()
					&& !(getMyPlayer().getAnimation() == 11786)
					|| !(getMyPlayer().getAnimation() == 11788)
					|| !(getMyPlayer().getAnimation() == 5713)
					|| !(getMyPlayer().getAnimation() == 5748)) {
				rest();
			} else {
				rest();
				while (getEnergy() < 100
						&& (getMyPlayer().getAnimation() == 11786)
						|| (getMyPlayer().getAnimation() == 11788)
						|| (getMyPlayer().getAnimation() == 5713)
						|| (getMyPlayer().getAnimation() == 5748)) {
					myAntiban();
					wait(15);
				}
				wait(random(300, 800));
			}

		} else if (roll > 990 && getInventoryCount() < 23) {
			checkMySkill();
		} else if (roll > 985 && getInventoryCount() < 23) {
			checkPlayer();
		} else if (roll > 960) {
			doCamera();
		} else if (roll > 940) {
			moveMouseSlightly();
		} else if (roll > 890) {
			openRandomTab();
		} else if (roll > 780) {
			checkMyFriends();
		}
		return false;
	}

	@Override
	public int loop() {
		return -1;
	}

}
